//
//  InviteViewController.m
//  FootPrint
//
//  Created by 秦 东方 on 4/29/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "InviteViewController.h"
#import "FriendSelCell.h"
#import "FriendDetailViewController.h"

@interface InviteViewController ()

@end

@implementation InviteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _parser = [[FriendParser alloc]init];
        _parser.inviteDelegate = self;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_parser inviteRequest:[GConfig getUserId]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:NOTIFY_HANDLE_INVITE object:nil];
}



- (void)didFinishInviteDelegate:(FriendInvitationResponseMessage*)result{
    if(result != nil && [result resultCode]==0){
        self.inviteList = [GToolUtil PBArr2NSArr:[result invitationList]];
        [_tableView reloadData];
        if([_inviteList count] == 0){
            [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFY_INVITE_DONE object:nil];
        }
    }
}
- (void)failInvite{

}

- (void)refreshData{

    [_parser inviteRequest:[GConfig getUserId]];
}

#pragma mark TableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _inviteList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *friendSelCellIdentifier = @"friendInviteCellIdentifier";
    FriendSelCell *cell = [tableView dequeueReusableCellWithIdentifier:friendSelCellIdentifier];
    
    
    if (cell == nil) {
        cell = [[[FriendSelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:friendSelCellIdentifier] autorelease];
    }
    
    UserMessage* user = [((FriendInvitationResponseMessage_Invitation*)[_inviteList objectAtIndex:indexPath.row]) user];

    [cell updateUserMsg:user];
    
    cell.redioImage.hidden = YES;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UserMessage* user = [((FriendInvitationResponseMessage_Invitation*)[_inviteList objectAtIndex:indexPath.row]) user];
    
    FriendDetailViewController * detail = [[[FriendDetailViewController alloc] initWithNibName:@"FriendDetailViewController" bundle:nil] autorelease];
    UINavigationController * navVc = self.navigationController;
    if (nil == navVc) {
        navVc = [[[UINavigationController alloc] initWithRootViewController:detail] autorelease];
        navVc.navigationBarHidden = YES;
        [ROOT_VC presentModalViewController:navVc animated:YES];
    } else {
        [navVc pushViewController:detail animated:YES];
    }
    detail.activityBtn.hidden = NO;
    [detail updateUserStatus:user];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTableView:nil];
    [super viewDidUnload];
}
- (IBAction)actionBack:(id)sender {
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}
@end
